<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2>系统登录</h2>
      </template>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="top"
        @submit.prevent="handleSubmit"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item class="mb-0">
          <el-button
            type="primary"
            native-type="submit"
            :loading="authStore.loading"
            block
            class="login-button"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import type { LoginForm } from '@/types'
import { useRouter } from 'vue-router'

const authStore = useAuthStore()
const formRef = ref<FormInstance>()

const form = ref<LoginForm>({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const router = useRouter()

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await authStore.login(form.value)
        router.push('/')
      } catch (error) {
        // 错误已在 store 中处理
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-card :deep(.el-card__header) {
  text-align: center;
}

.login-card h2 {
  margin: 0;
  font-weight: 500;
  color: #303133;
}

.login-button {
  
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to right, #409eff, #36a3ff);
  border: none;
  transition: all 0.3s ease;
  display: flex;
  margin-left: auto;
  width: fit-content;
  min-width: 100px;
}

.login-button:hover:not([disabled]) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  background: linear-gradient(to right, #36a3ff, #409eff);
}

.login-button:active:not([disabled]) {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(64, 158, 255, 0.4);
}
</style> 